<template>
  <div class="app-container">
    <!-- 条件查询 -->
    <el-card class="box-card">
      <el-form :inline="true" :model="query" size="mini">
        <el-form-item label="标题">
          <el-input v-model.trim="query.title" clearable placeholder="请输入图片标题"/>
        </el-form-item>
        <el-form-item label="用户名">
          <el-input v-model.trim="query.username" clearable placeholder="请输入用户名"/>
        </el-form-item>
        <!-- 状态：（ 0:待审核; 1 审核通过; 2 审核未通过; 3 留存） -->
        <el-form-item label="状态">
          <el-select v-model="query.status" clearable style="width: 120px">
            <el-option label="待审核" :value="0"/>
            <el-option label="审核通过" :value="1"/>
            <el-option label="审核未通过" :value="2"/>
            <el-option label="留存" :value="3"/>
          </el-select>
        </el-form-item>
        <!--  审核未通过的原因：(0 无；1 涉嫌侵权;2 涉嫌违法; 3 内容不雅; 4 血腥暴力;5 涉嫌虚假宣传; -->
        <el-form-item label="审核结果">
          <el-select v-model="query.fail" clearable style="width: 120px">
            <el-option label="无" :value="0"/>
            <el-option label="涉嫌侵权" :value="1"/>
            <el-option label="涉嫌违法" :value="2"/>
            <el-option label="内容不雅" :value="3"/>
            <el-option label="血腥暴力" :value="4"/>
            <el-option label="涉嫌虚假宣传" :value="5"/>
          </el-select>
        </el-form-item>
        <!-- 位置：(0 首页顶部；1 首页右则；2 帖子正文下) -->
        <el-form-item label="位置">
          <el-select v-model="query.position" clearable style="width: 120px">
            <el-option label="首页顶部" :value="0"/>
            <el-option label="首页右则" :value="1"/>
            <el-option label="帖子正文下" :value="2"/>
          </el-select>
        </el-form-item>
        <el-form-item label="图片ID">
          <el-input v-model.trim="query.id" clearable placeholder="请输入图片ID"/>
        </el-form-item>
        <el-form-item label="用户ID">
          <el-input v-model.trim="query.userId" clearable placeholder="请输入用户ID"/>
        </el-form-item>
        <el-form-item label="链接地址">
          <el-input v-model.trim="query.looperLink" clearable placeholder="请输入链接地址"/>
        </el-form-item>
        <el-form-item label="赞助单号">
          <el-input v-model.trim="query.payNumber" clearable placeholder="请输入赞助单号"/>
        </el-form-item>
        <el-form-item label="赞助金额">
          <el-select v-model="query.amount" clearable style="width: 120px">
            <el-option label="倒序" :value="1"/>
          </el-select>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="queryData">查询</el-button>
            <el-button icon="el-icon-refresh" @click="reload">重置</el-button>
          </el-form-item>
        </el-form-item>
        <el-form-item label="日期范围">
          <el-date-picker
            clearable
            v-model="query.begin"
            placeholder="开始时间"
            value-format="yyyy-MM-dd"
          />
        </el-form-item>
        <el-form-item label="-">
          <el-date-picker
            clearable
            v-model="query.end"
            placeholder="结束时间"
            value-format="yyyy-MM-dd"
          />
        </el-form-item>
        <el-form-item>
          <el-button v-permission="'looper:search'" icon="el-icon-search" type="primary" @click="queryData">查询</el-button>
          <el-button v-permission="'looper:search'" icon="el-icon-refresh" @click="reload">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <!-- 表格显示列表数据 -->
    <!-- height="750" 为了固定表头 -->
    <el-table
      :data="list"
      stripe
      border
      height="650"
    >
      <el-table-column label="序号" width="70" fixed align="center">
        <template slot-scope="scope">
          <!--  page.current 当前页码；page.size 每页记录数 -->
          {{ (current - 1) * size + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column prop="username" label="投放人" align="center" width="120"/>
      <el-table-column align="center" prop="looperUrl" label="图片" width="100">
        <template slot-scope="scope">
          <el-image
            :src="scope.row.looperUrl"
            :preview-src-list="[scope.row.looperUrl]"
          />
        </template>
      </el-table-column>
      <el-table-column prop="title" label="标题" align="center" width="200"/>
      <!-- 位置：(0 首页轮播图；1 首页右则；2 帖子正文下) -->
      <el-table-column prop="position" label="位置" align="center" width="120">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.position == 0" type="danger" effect="dark">首页顶部</el-tag>
          <el-tag v-if="scope.row.position == 1" type="info" effect="dark">页面右则</el-tag>
          <el-tag v-if="scope.row.position == 2" type="primary" effect="dark">帖子正文下</el-tag>
        </template>
      </el-table-column>
      <!-- <el-table-column prop="looperLink" label="链接网址" align="center" width="240"/>-->
      <el-table-column prop="looperLink" label="链接网址" align="center" width="200">
        <template slot-scope="scope">
          <a :href="scope.row.looperLink">{{ scope.row.looperLink }}</a>
        </template>
      </el-table-column>
      <!-- 状态：（ 0:待审核; 1 审核通过播放中; 2 审核未通过; 3  留存） -->
      <el-table-column align="center" prop="status" label="状态" width="150">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status == 0" type="" effect="dark">待审核</el-tag>
          <el-tag v-if="scope.row.status == 1" type="success">审核通过</el-tag>
          <el-tag v-if="scope.row.status == 2" type="info">审核未通过</el-tag>
          <el-tag v-if="scope.row.status == 3" type="info">留存</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="updateTime" label="更新时间" width="160px"/>
      <!-- fail 审核未通过的原因：(0 无；1 涉嫌侵权；2 涉嫌违法；3 内容不雅；4 血腥暴力；5 涉嫌虚假宣传； -->
      <el-table-column align="center" prop="fail" label="审核结果" width="150">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.fail == 0" type="success">无</el-tag>
          <el-tag v-if="scope.row.fail == 1" type="danger">涉嫌侵权</el-tag>
          <el-tag v-if="scope.row.fail == 2" type="danger">涉嫌违法</el-tag>
          <el-tag v-if="scope.row.fail == 3" type="danger">内容不雅</el-tag>
          <el-tag v-if="scope.row.fail == 4" type="danger">血腥暴力</el-tag>
          <el-tag v-if="scope.row.fail == 5" type="danger">涉嫌虚假宣传</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="createTime" label="创建时间" width="160px"/>
      <el-table-column prop="amount" label="赞助金额" align="center"/>
      <el-table-column prop="payNumber" label="赞助单号" align="center" width="200"/>
      <el-table-column prop="userId" label="用户ID" align="center" width="200"/>
      <el-table-column prop="id" label="图片ID" align="center" width="200"/>
      <el-table-column align="center" label="操作" min-width="300px" fixed="right">
        <template slot-scope="scope">
          <!-- 1、打开审核窗口，只有 status == 0 待审核状态，才显示审核按钮  v-permission="'article:audit'" -->
          <el-button
            v-permission="'looper:audit'"
            :disabled="scope.row.status == 1"
            size="mini"
            type="primary"
            @click="auditPass(scope.row.id)"
          >审核通过
          </el-button>
          <el-button
            v-permission="'looper:audit'"
            :disabled="scope.row.status == 2"
            size="mini"
            type="info"
            @click="openNoPass(scope.row.id,scope.row.title)"
          >审核不通过
          </el-button>
          <!-- 5、根据ID删除文章 v-permission="'article:delete'" -->
          <el-button v-permission="'looper:delete'" type="danger" size="mini" @click="handleDelete(scope.row.id)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <el-pagination
      align="center"
      background
      :current-page="current"
      :page-sizes="[10, 20,30,50,100]"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <!-- 投图赞助-审核不通过-->
    <el-dialog
      :visible.sync="dialogVisible"
      width="50%"
      :before-close="handleClose"
    >
      <!-- 审核未通过的原因：(0 无；1 涉嫌侵权；2 涉嫌违法；3 内容不雅；4 血腥暴力；5 涉嫌虚假宣传；) -->
      <div align="center">
        <h2>《{{ title }}》 <i class="el-icon-s-check"/> 审核未通过的原因</h2>
        <el-radio-group v-model="fail">
          <!-- <el-radio-button :label="0">无</el-radio-button>-->
          <el-radio-button :label="1">涉嫌侵权</el-radio-button>
          <el-radio-button :label="2">涉嫌违法</el-radio-button>
          <el-radio-button :label="3">内容不雅</el-radio-button>
          <el-radio-button :label="4">血腥暴力</el-radio-button>
          <el-radio-button :label="5">涉嫌虚假宣传</el-radio-button>
        </el-radio-group>
      </div>
      <div align="center" style="margin-top: 50px;">
        <el-button v-permission="'looper:audit'" @click="dialogVisible = false">取 消</el-button>
        <el-button v-permission="'looper:audit'" type="primary" @click="auditNoPass">确 定</el-button>
      </div>
      <br>
    </el-dialog>
  </div>
</template>
<script>
import api from '@/api/patron'

export default {
  name: 'Looper',
  data() {
    return {
      list: [],
      current: 1,
      size: 10,
      total: 0,
      query: {},
      // 审核不通过的原因
      dialogVisible: false,
      fail: 0,
      id: null,
      title: ''
    }
  },
  created() {
    // 数据列表
    this.fetchData()
  },
  methods: {
    // 获取条件分页列表数据
    async fetchData() {
      const { data } = await api.getLooperList(this.current, this.size, this.query)
      // console.log('data', data)
      this.list = data.records
      this.total = Number(data.total)
    },
    // 切换每页显示多少条记录
    handleSizeChange(size) {
      this.size = size
      this.fetchData()
    },
    // 切换当前页码
    handleCurrentChange(current) {
      this.current = current
      this.fetchData()
    },
    // 条件查询
    queryData() {
      this.current = 1
      this.fetchData()
    },
    // 重置表单
    reload() {
      this.query = {}
      this.fetchData()
    },
    // 审核通过
    auditPass(id) {
      this.$confirm('确定轮播图片 审核通过 吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 发送请求
        api.auditPass(id).then(response => {
          if (response.code === 200) {
            this.fetchData()
            this.$message.success(response.message)
          } else {
            this.$message.error(response.message)
          }
        })
      }).catch(() => {
      })
    },
    // 打开审核不通过原因对话框
    openNoPass(id, title) {
      this.id = id
      this.title = title
      this.fail = 0
      this.dialogVisible = true
    },
    // 审核不通过
    auditNoPass() {
      if (this.fail === 0) {
        this.$message.error('请选择审核不通过的原因')
        return
      }
      api.auditNoPass(this.id, this.fail).then(response => {
        if (response.code === 200) {
          this.fetchData()
          this.fail = 0
          this.dialogVisible = false
          this.$message.success(response.message)
        } else {
          this.$message.error(response.message)
        }
      })
    },
    // 关闭审核不通过原因对话框
    handleClose() {
      this.dialogVisible = false
    },
    // 根据ID删除
    handleDelete(id) {
      this.$confirm('确定要删除这条记录吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 发送请求
        api.deleteByLooperId(id).then(response => {
          if (response.code === 200) {
            this.fetchData()
            this.$message.success(response.message)
          } else {
            this.$message.error(response.message)
          }
        })
      }).catch(() => {
      })
    }
  }
}
</script>
<style scoped>
h2 {
  margin-bottom: 30px;
}

.el-image {
  border-radius: 4px;
}

.el-pagination {
  margin: 20px 0 50px;
}
</style>
